﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE.Tool.UserControls;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE.Tool.Forms
{
	public partial class frm_BorrowEdit : DevExpress.XtraEditors.XtraForm
	{
		public int Id;
		public DateTime Date;
		public string ToolId;
		public int Quantity;
		public string Staff;
		public string Machine;
		public int ToolOrQTool;
		public string Note;
		private ucBorrow parent;
		private DataTable dt = new DataTable();

		public frm_BorrowEdit(ucBorrow fBorrow)
		{
			InitializeComponent();
			parent = fBorrow;
			this.CancelButton = this.btTCancel;
		}

		private void rdTool_CheckedChanged(object sender, EventArgs e)
		{
			BindDataTool(ToolConstants.Configuration.ToolType.IsTool);
			ToolOrQTool = ToolConstants.Configuration.ToolType.IsTool;
		}

		private void rdQTool_CheckedChanged(object sender, EventArgs e)
		{
			BindDataTool(ToolConstants.Configuration.ToolType.IsQTool);
			ToolOrQTool = ToolConstants.Configuration.ToolType.IsQTool;
		}

		private void XtraForm1_Load(object sender, EventArgs e)
		{
			lbID.Text = Id.ToString();
			dtpTDate.Value = Date;
			txtTQuantity.Text = Quantity.ToString();
			memoTNote.Text = Note;
			BindDataStaff();
			BindDataMachine();

			if(ToolOrQTool == 0)
			{
				rdTool.Checked = true;
			}
			else
			{
				rdQTool.Checked = true;
			}

			try
			{
				cmbToolID.SelectedValue = ToolId;
				if(cmbToolID.SelectedItem == null)
				{
					cmbToolID.SelectedItem = cmbToolID.Items[0];
				}
			}
			catch(Exception)
			{
				cmbToolID.SelectedItem = cmbToolID.Items[0];
			}

		}

		private void btTUpdate_Click_1(object sender, EventArgs e)
		{
			UpdateData();
		}

		private void btTCancel_Click_1(object sender, EventArgs e)
		{
			this.Close();
		}

        private void cmbToolID_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (cmbToolID.SelectedItem != cmbToolID.Items[0])
            {
                string fieldName = ToolConstants.StoreProcedure.FieldName.DaoCu;
                string where = ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.FTonKhoTongHop;

                int instockAtpresent = Utilities.CheckInput(fieldName, storedName, where);

                if (instockAtpresent <= 0)
                {
                    MessageBox.Show(ToolConstants.Message.Borrow.ToolOutStock, ToolConstants.Form.MessageBox.Notice, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cmbToolID.SelectedItem = cmbToolID.Items[0];
                }
                else
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.Borrow.ToolInStock, cmbToolID.SelectedValue, instockAtpresent.ToString()), ToolConstants.Form.MessageBox.Notice, MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
        }

        private void cmbMachine_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (cmbMachine.SelectedItem != cmbMachine.Items[0])
            {
                string fieldName = ToolConstants.StoreProcedure.FieldName.Total;
                string where = ToolConstants.StoreProcedure.FieldName.MachineID + "='" + cmbMachine.SelectedValue + "' and " + ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.QTotalMachineAtFactoryBorrow;
                string tempTableName = ToolConstants.StoreProcedure.TemporaryTables.QTotalMachineAtFactoryBorrow;

                int number = Utilities.CheckInput(fieldName, storedName, where, true, tempTableName);

                if (number == 0)
                    MessageBox.Show(string.Format(ToolConstants.Message.Borrow.MachineNotBorrow, cmbMachine.SelectedValue, cmbToolID.SelectedValue), ToolConstants.Form.MessageBox.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.Borrow.MachineBorrow, cmbMachine.SelectedValue, cmbToolID.SelectedValue, number.ToString()), ToolConstants.Form.MessageBox.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
        }

		#region Private methods

		private bool GetDataTool(Borrow o)
		{
			#region Check valid

			if(Utilities.IsNullOrEmpty(dtpTDate.Value.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Borrow.DateCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(cmbToolID.SelectedValue.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Borrow.ToolIDCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

			if(Utilities.IsNullOrEmpty(cmbStaff.SelectedValue.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Borrow.StaffCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}


			if(Utilities.IsNullOrEmpty(cmbMachine.SelectedValue.ToString()))
			{
				MessageBox.Show(ToolConstants.Message.Borrow.MachineCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}


			if(Utilities.IsNullOrEmpty(txtTQuantity.Text.Trim()))
			{
				MessageBox.Show(ToolConstants.Message.Borrow.QuantityCantEmpty, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
				return false;
			}

            if (int.Parse(txtTQuantity.Text) <= 0)
            {
                MessageBox.Show(ToolConstants.Message.Borrow.QuantityMustGreaterZero, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTQuantity.Focus();
                return false;

            }

            if (int.Parse(txtTQuantity.Text) > 0)
            {
                string fieldName = ToolConstants.StoreProcedure.FieldName.DaoCu;
                string where = ToolConstants.StoreProcedure.FieldName.ToolID + "='" + cmbToolID.SelectedValue + "'";
                string storedName = ToolConstants.StoreProcedure.FTonKhoTongHop;

                int instockAtpresent = Utilities.CheckInput(fieldName, storedName, where);

                if (instockAtpresent < int.Parse(txtTQuantity.Text))
                {
                    MessageBox.Show(string.Format(ToolConstants.Message.Borrow.WrongQuantity, cmbToolID.SelectedValue, instockAtpresent.ToString(), txtTQuantity.Text), ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtTQuantity.Text = "";
                    txtTQuantity.Focus();
                    return false;

                }

            }
			#endregion

			o.ID = Id;
			o.Date = dtpTDate.Value;
			o.ToolID = cmbToolID.SelectedValue.ToString();
			o.Quantity = int.Parse(txtTQuantity.Text);
			o.StaffID = cmbStaff.SelectedValue.ToString();
			o.MachineID = cmbMachine.SelectedValue.ToString();
			o.ToolOrQTool = ToolOrQTool;
			o.Note = memoTNote.Text;

			return true;
		}

		private void BindDataTool(int type)
		{
			ToolHelper helper = new ToolHelper(new Tools());
			helper.ValueObject.ToolOrQTool = type;
			dt = helper.GetAllByToolType();
			DataRow row = dt.NewRow();

			row[ToolConstants.Entities.Tool.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.Tool.FieldName.ToolID] = null;
			dt.Rows.InsertAt(row, 0);

			cmbToolID.DataSource = dt;
			cmbToolID.DisplayMember = ToolConstants.Entities.Tool.FieldName.FullName;
			cmbToolID.ValueMember = ToolConstants.Entities.Tool.FieldName.ToolID;

		}

		private void BindDataStaff()
		{
			StaffHelper helper = new StaffHelper(new Staff());
			DataTable dt = helper.GetAll();
			DataRow row = dt.NewRow();

			cmbStaff.DataSource = dt;
			cmbStaff.DisplayMember = ToolConstants.Entities.Staff.FieldName.FullName;
			cmbStaff.ValueMember = ToolConstants.Entities.Staff.FieldName.StaffID;
            row[ToolConstants.Entities.Staff.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.Staff.FieldName.StaffID] = null;
			dt.Rows.InsertAt(row, 0);
			cmbStaff.SelectedValue = Staff;
			if(cmbStaff.SelectedItem == null)
			{
				cmbStaff.SelectedItem = cmbStaff.Items[0];
			}
		}

		private void BindDataMachine()
		{
			MachineHelper helper = new MachineHelper(new Machine());
			DataTable dt = helper.GetAll();
			DataRow row = dt.NewRow();

			cmbMachine.DataSource = dt;
			cmbMachine.DisplayMember = ToolConstants.Entities.Machine.FieldName.FullName;
			cmbMachine.ValueMember = ToolConstants.Entities.Machine.FieldName.MachineID;
            row[ToolConstants.Entities.Machine.FieldName.FullName] = "Select...";
			row[ToolConstants.Entities.Machine.FieldName.MachineID] = null;
			dt.Rows.InsertAt(row, 0);
			cmbMachine.SelectedValue = Machine;

			if(cmbMachine.SelectedItem == null)
			{
				cmbMachine.SelectedItem = cmbMachine.Items[0];
			}
		}

		private void UpdateData()
		{
			BorrowHelper helper = new BorrowHelper(new Borrow());
			if(GetDataTool(helper.ValueObject))
			{

				Utilities.Timer timer = new Utilities.Timer();

				if(helper.Update() > 0)
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Blue, ToolConstants.Message.UpdateSuccess);
				}
				else
				{
					timer.lb= lblStatus;
					timer.ShowHideLabel(lblStatus, 3000, Color.Red, ToolConstants.Message.UpdateFailed);
				}
				parent.BindDatatoGrid();

			}
		}

		#endregion


	}
}